home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / graphics / tierra40.arj / TIERRA / PORTABLE.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  13KB  |  549 lines

  1. /* portable.c   9-9-92  conditionally compiled functions for portability */
  2. /* Tierra Simulator V4.0: Copyright (c) 1991, 1992 Tom Ray & Virtual Life */
  3.  
  4. #ifndef lint
  5. static char     portable_sccsid[] = "@(#)portable.c    1.5        7/21/92";
  6. #endif
  7.  
  8. /* #define ARG   */
  9.  
  10. #include "license.h"
  11.  
  12. #include <stdio.h>
  13. #include "tierra.h"
  14. #include "extern.h"
  15.  
  16. #ifdef ALCOMM
  17. #include <mlayer.h>
  18. #endif
  19.  
  20. #ifdef MEM_CHK
  21. #include <memcheck.h>
  22. #endif
  23.  
  24. #ifdef __TURBOC__
  25. /* I8s Hp  GP = (I8s Hp) 0x9bff0008; */ /* leaves about 16K free */
  26. I8s Hp  GP = (I8s Hp) 0x9efd0008; /* leaves about  4K free */
  27. #endif /* __TURBOC__ */
  28.  
  29. /* free for arrays not greater than 64K */
  30. void tfree(ptr)
  31. I8s Fp  ptr;
  32. {
  33. #ifdef __TURBOC__
  34.     free(ptr);
  35. #endif /* __TURBOC__ */
  36.  
  37. #ifdef OS2_MC
  38.     DosFreeSeg((I16u) ptr >> 16);
  39. #endif /* OS2_MC */
  40.  
  41. #ifdef unix
  42.  
  43. #ifdef ALCOMM
  44.     (void) ALFree( (char *)ptr );
  45. #else /* ALCOMM */
  46.     free(ptr);
  47. #endif  /* ALCOMM */
  48.  
  49.  
  50. #endif /* unix */
  51.  
  52. #ifdef IBM3090
  53.     free(ptr);
  54. #endif /* IBM3090 */
  55. }
  56.  
  57. /* free for arrays of greater than 64K */
  58. void thfree(ptr)
  59. I8s Hp  ptr;
  60. {
  61. #ifdef __TURBOC__
  62.     farfree(ptr);
  63. #endif
  64.  
  65. #ifdef OS2_MC
  66.     DosFreeSeg((I16u) ptr >> 16);
  67. #endif
  68.  
  69. #ifdef unix
  70.  
  71. #ifdef ALCOMM
  72.     (void) ALFree( (char *)ptr );
  73. #else
  74.     free(ptr);
  75. #endif  /* ALCOMM */
  76.  
  77. #endif
  78.  
  79. #ifdef IBM3090
  80.     free(ptr);
  81. #endif
  82. }
  83.  
  84. /* recalloc for Unix, or for DOS arrays not greater than 64K */
  85. I8s Fp trecalloc(ptr, nsiz, osiz)
  86. I8s Fp  ptr;
  87. I32u    nsiz; /* new array size */
  88. I32u    osiz; /* old array size */
  89. {
  90. #ifdef __TURBOC__
  91.     I8s  Fp tp, Hp hp, swapped = 0;
  92.     I32u  swapsiz = (osiz < nsiz) ? osiz : nsiz;
  93.     FILE  *iof;
  94.  
  95. #ifndef ARG 
  96.     hp = tp = (I8s  Fp) calloc(nsiz, 1);
  97.     while ((hp + nsiz) > GP || tp == NULL)
  98.     {   if (tp)
  99.         {   tfree(tp);
  100.             tp = NULL;
  101.         }
  102.         if (!GeneBnker || !Swap || !reaped)
  103.             break;
  104.         if (ptr)
  105.         {   iof = fopen("swap","wb");
  106.             tfwrite(ptr, 1, swapsiz, iof);
  107.             fclose(iof);
  108.             tfree(ptr);
  109.             ptr = NULL;
  110.             swapped = 1;
  111.         }
  112.         if (!gq_swap())
  113.             FEExit(-123);
  114.         hp = tp = (I8s  Fp) calloc(nsiz, 1);
  115.     }
  116.     if (tp)
  117.     {   if (swapped)
  118.         {   iof = fopen("swap","rb");
  119.             tfread(tp, 1, swapsiz, iof);
  120.             fclose(iof);
  121.             unlink("swap");
  122.         }
  123.         else if (ptr)
  124.         {   memcpy(tp, ptr, swapsiz);
  125.             tfree(ptr);
  126.             ptr = NULL;
  127.         }
  128.     }
  129. #else /* ARG */
  130.     tp = (I8s  Fp) realloc(ptr, (size_t) nsiz);
  131. #endif /* ARG */
  132.  
  133.     return  tp;
  134. #endif /* __TURBOC__ */
  135.  
  136. #ifdef unix
  137.     I8s *  tp;
  138.     I32u  swapsiz = (osiz < nsiz) ? osiz : nsiz;
  139.  
  140. #ifdef ALCOMM
  141.     tp = (I8s *) ALCalloc(nsiz, 1);
  142. #else /* ALCOMM */
  143.     tp = (I8s *) calloc(nsiz, 1);
  144. #endif /* ALCOMM */
  145.     if(tp == NULL)
  146.     {
  147. #ifdef ARG 
  148.         fprintf(stderr, "Tierra trecalloc() error: realloc failed");
  149.         exit(errno);
  150. #else /* ARG */
  151.         FEError(-700,EXIT,NOWRITE,
  152.             "Tierra trecalloc() error: realloc failed");
  153. #endif /* ARG */
  154.     }
  155.     else if (ptr)
  156.     {   memcpy(tp, ptr, swapsiz);
  157.         tfree(ptr);
  158.         ptr = NULL;
  159.     }
  160.     return tp;
  161.  
  162. #endif /* unix */
  163.  
  164. #ifdef OS2_MC
  165.     I16u     NumSegs, Remain, Selector;
  166.     I8s  Fp  tp;
  167.  
  168.     NumSegs = (I16u) nsiz / 65536ul;
  169.     Remain  = (I16u) nsiz % 65536ul;
  170.     Selector = ((I16u) ptr >> 16);
  171.     DosReallocHuge(NumSegs,Remain,Selector);
  172.     return (void Fp) ptr;
  173. #endif /* OS2_MC */
  174.  
  175. #ifdef IBM3090
  176.     return (I8s  *) realloc(ptr,nsiz);
  177. #endif /* IBM3090 */
  178. }
  179.  
  180. /* huge recalloc for arrays of greater than 64K */
  181. I8s Hp threcalloc(ptr, nsiz, osiz)
  182. I8s Hp  ptr;
  183. I32u    nsiz; /* new array size */
  184. I32u    osiz; /* old array size */
  185. {
  186. #ifdef __TURBOC__
  187.     I8s   Hp tp, Hp dp, Hp sp, Hp hp, swapped = 0;
  188.     I32s  segs, rem, i;
  189.     I32u  swapsiz = (osiz < nsiz) ? osiz : nsiz;
  190.     FILE  *iof;
  191.  
  192. #ifndef ARG 
  193.     hp = tp = (I8s  Hp) farcalloc(nsiz, 1);
  194.     while ((hp + nsiz) > GP || tp == NULL)
  195.     {   if (tp)
  196.         {   thfree(tp);
  197.             tp = NULL;
  198.         }
  199.         if (!GeneBnker || !Swap || !reaped)
  200.             break;
  201.         if (ptr)
  202.         {   iof = fopen("swap","wb");
  203.             tfwrite(ptr, 1, swapsiz, iof);
  204.             fclose(iof);
  205.             thfree(ptr);
  206.             ptr = NULL;
  207.             swapped = 1;
  208.         }
  209.         if (!gq_swap())
  210.             FEExit(-123);
  211.         hp = tp = (I8s  Hp) farcalloc(nsiz, 1);
  212.     }
  213.     if (tp)
  214.     {   if (swapped)
  215.         {   iof = fopen("swap","rb");
  216.             tfread(tp, 1, swapsiz, iof);
  217.             fclose(iof);
  218.             unlink("swap");
  219.         }
  220.         else if (ptr)
  221.         {   segs = swapsiz / (I32s) (UINT_MAX - 1);
  222.             rem  = swapsiz % (I32s) (UINT_MAX - 1);
  223.             dp = tp; sp = ptr;
  224.             if(segs) for(i = 0; i < segs; i++)
  225.             {   memcpy(dp, sp, (UINT_MAX - 1));
  226.                 dp += (UINT_MAX - 1);
  227.                 sp += (UINT_MAX - 1);
  228.             }
  229.             if(rem)
  230.                 memcpy(dp, sp, rem);
  231.             thfree(ptr);
  232.             ptr = NULL;
  233.         }
  234.     }
  235. #else /* ARG */
  236.     tp = (I8s  Hp) farrealloc(ptr, nsiz);
  237. #endif /* ARG */
  238.     return  tp;
  239. #endif /* __TURBOC__ */
  240.  
  241. #ifdef unix
  242.     I8s *  tp;
  243.     I32u  swapsiz = (osiz < nsiz) ? osiz : nsiz;
  244.  
  245. #ifdef ALCOMM
  246.     tp = (I8s *) ALCalloc(nsiz, 1);
  247. #else
  248.     tp = (I8s *) calloc(nsiz, 1);
  249. #endif        /* ALCOMM */
  250.     if(tp == NULL)
  251.     {
  252. #ifdef ARG 
  253.         fprintf(stderr, "Tierra threcalloc() error: recalloc failed");
  254.         exit(errno);
  255. #else
  256.         FEError(-701,EXIT,NOWRITE,
  257.             "Tierra threcalloc() error: recalloc failed");
  258. #endif
  259.     }
  260.     else if (ptr)
  261.     {   memcpy(tp, ptr, swapsiz);
  262.         tfree(ptr);
  263.         ptr = NULL;
  264.     }
  265.     return tp;
  266.  
  267. #endif /* unix */
  268.  
  269. #ifdef OS2_MC
  270.     I16u     NumSegs, Remain, Selector;
  271.     I8s  Hp  tp;
  272.  
  273.     NumSegs = (I16u) nsiz / 65536ul;
  274.     Remain  = (I16u) nsiz % 65536ul;
  275.     Selector = ((I16u) ptr >> 16);
  276.     DosReallocHuge(NumSegs,Remain,Selector);
  277.     return (void Hp) ptr;
  278. #endif
  279.  
  280. #ifdef IBM3090
  281.     return (I8s  *) realloc(ptr,nsiz);
  282. #endif
  283. }
  284.  
  285. /* calloc for arrays not greater than 64K */
  286. I8s Fp tcalloc(num, siz)
  287. I32u   num;
  288. I32u   siz;
  289. {
  290. #ifdef __TURBOC__
  291.     I8s  Fp tp, Hp hp;
  292.  
  293. #ifndef ARG 
  294.     hp = tp = (I8s  Fp) calloc(num, siz);
  295.     while ((hp + (num * siz)) > GP || tp == NULL)
  296.     {   if (tp)
  297.         {   tfree(tp);
  298.             tp = NULL;
  299.         }
  300.         if (!GeneBnker || !Swap || !reaped)
  301.             break;
  302.         if (!gq_swap())
  303.             FEExit(-123);
  304.         hp = tp = (I8s  Fp) calloc(num, siz);
  305.     }
  306. #else /* ARG */
  307.     tp = (I8s  Fp) calloc(num,siz);
  308. #endif /* ARG */
  309.  
  310.     return  tp;
  311. #endif /* __TURBOC__ */
  312.  
  313. #ifdef unix
  314.     I8s *  tp;
  315.  
  316. #ifdef ALCOMM
  317.     tp = (I8s *) ALCalloc(num, siz);
  318. #else /* ALCOMM */
  319.     tp = (I8s *) calloc(num, siz);
  320. #endif /* ALCOMM */
  321.     if(tp == NULL)
  322.     {
  323. #ifdef ARG 
  324.         fprintf(stderr, "Tierra tcalloc() error: calloc failed");
  325.         exit(errno);
  326. #else /* ARG */
  327.         FEError(-703,EXIT,NOWRITE, "Tierra tcalloc() error: calloc failed");
  328. #endif /* ARG */
  329.     }
  330.     return tp;
  331.  
  332. #endif /* unix */
  333.  
  334. #ifdef OS2_MC
  335.     I32u     HugeSize, i, ASize;
  336.     I16u     NumSegs, Remain, rc;
  337.     I8s Hp  tp;
  338.     SEL      Selector;
  339.  
  340.     HugeSize = num * siz;
  341.     i = HugeSize / 65536ul;
  342.     NumSegs = (I16u) i;
  343.     Remain  = (I16u) HugeSize % 65536ul;
  344.     rc = DosAllocHuge(NumSegs,Remain,&Selector,3 * NumSegs,0);
  345.     if(rc)
  346.     {
  347. #ifdef ARG
  348.         fprintf(stderr, "Tierra tcalloc() DosAllocHuge error = %u", rc);
  349.         exit(errno);
  350. #else  /* ARG */
  351.         FEError(-702,EXIT,NOWRITE,
  352.             "Tierra tcalloc() DosAllocHuge error = %u", rc);
  353. #endif /* ARG */
  354.     }
  355.     tp = (I8s Fp) ((I32u ) Selector << (I32u) 16);
  356.     for(i = 0; i < HugeSize; i++)
  357.     {   *(tp + i) = (I8s) 0;
  358.     }
  359.     return (I8s Fp) tp;
  360. #endif /* OS2_MC */
  361.  
  362. #ifdef IBM3090
  363.     return (I8s  *) calloc(num,siz);
  364. #endif /* IBM3090 */
  365. }
  366.  
  367. /* calloc for arrays of greater than 64K */
  368. I8s Hp thcalloc(num, siz)
  369. I32u   num;
  370. I32u   siz;
  371. {
  372. #ifdef __TURBOC__
  373.     I8s  Hp tp, Hp hp;
  374.  
  375. #ifndef ARG 
  376.     hp = tp = (I8s  Hp) farcalloc(num, siz);
  377.     while ((hp + (num * siz)) > GP || tp == NULL)
  378.     {   if (tp)
  379.         {   thfree(tp);
  380.             tp = NULL;
  381.         }
  382.         if (!GeneBnker || !Swap || !reaped)
  383.             break;
  384.         if (!gq_swap())
  385.             FEExit(-123);
  386.         hp = tp = (I8s  Hp) farcalloc(num, siz);
  387.     }
  388. #else  /* ARG */
  389.     tp = (I8s  Hp) farcalloc(num,siz);
  390. #endif /* ARG */
  391.  
  392.     return  tp;
  393. #endif /* __TURBOC__ */
  394.  
  395. #ifdef OS2_MC
  396.     I32u     HugeSize, i, ASize;
  397.     I16u     NumSegs, Remain, rc;
  398.     I8s Hp  tp;
  399.     SEL      Selector;
  400.  
  401.     HugeSize = num * siz;
  402.     i = HugeSize / 65536ul;
  403.     NumSegs = (I16u) i;
  404.     Remain  = (I16u) HugeSize % 65536ul;
  405.     rc = DosAllocHuge(NumSegs,Remain,&Selector,3 * NumSegs,0);
  406.     if(rc)
  407.     {
  408. #ifdef ARG 
  409.         fprintf(stderr, "Tierra thcalloc() DosAllocHuge error = %u", rc);
  410.         exit(errno);
  411. #else  /* ARG */
  412.         FEError(-704,EXIT,NOWRITE,
  413.             "Tierra thcalloc() DosAllocHuge error = %u", rc);
  414. #endif /* ARG */
  415.     }
  416.     tp = (I8s Hp) ((I32u ) Selector << (I32u) 16);
  417.     for(i = 0; i < HugeSize; i++)
  418.     {   *(tp + i) = (I8s) 0;
  419.     }
  420.     return (I8s Hp) tp;
  421. #endif /* OS2_MC */
  422.  
  423. #ifdef unix
  424.     I8s *  tp;
  425.  
  426. #ifdef ALCOMM
  427.     tp = (I8s *) ALCalloc(num, siz);
  428. #else  /* ALCOMM */
  429.     tp = (I8s *) calloc(num, siz);
  430. #endif /* ALCOMM */
  431.     if(tp == NULL)
  432.     {
  433. #ifdef ARG 
  434.         fprintf(stderr, "Tierra thcalloc() error: calloc failed");
  435.         exit(errno);
  436. #else  /* ARG */
  437.         FEError(-705,EXIT,NOWRITE, "Tierra thcalloc() error: calloc failed");
  438. #endif /* ARG */
  439.     }
  440.     return tp;
  441.  
  442. #endif /* unix */
  443.  
  444. #ifdef IBM3090
  445.     return (I8s  *) calloc(num,siz);
  446. #endif /* IBM3090 */
  447. }
  448.  
  449. I32u tfread(ptr, size, n, stream)
  450. I8s Hp  ptr;
  451. I32s  size, n;
  452. FILE  *stream;
  453. {   I32u  r_size = 0;
  454. #ifdef __TURBOC__
  455.     I32s  segs, rem, i = 0;
  456.  
  457.     segs = n / (I32s) UINT_MAX;
  458.     rem  = n % (I32s) UINT_MAX;
  459.     if(segs) for(i = 0; i < segs; i++)
  460.         r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
  461.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  462.     if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
  463.             (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  464. #endif /* __TURBOC__ */
  465.  
  466. #ifdef OS2_MC
  467.     I32s  segs, rem, i = 0;
  468.  
  469.     segs = n / (I32s) UINT_MAX;
  470.     rem  = n % (I32s) UINT_MAX;
  471.     if(segs) for(i = 0; i < segs; i++)
  472.         r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
  473.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  474.     if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
  475.             (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  476. #endif /* OS2_MC */
  477.  
  478. #ifdef unix
  479.     r_size = fread(ptr, size, n, stream);
  480. #endif /* unix */
  481.  
  482. #ifdef IBM3090
  483.     r_size = fread(ptr, size, n, stream);
  484. #endif /* IBM3090 */
  485.  
  486.     if(r_size != n)
  487.     {   
  488. #ifdef ARG 
  489.         fprintf(stderr, 
  490.         "Tierra tfread() inconsistency: n = %ld  r_size = %ld", n, r_size);
  491.         exit(errno);
  492. #else
  493.     FEError(-706,NOEXIT,NOWRITE,
  494.         "Tierra tfread() inconsistency: n = %ld  r_size = %ld", n, r_size);
  495. #endif
  496.     }
  497.     return r_size;
  498. }
  499.  
  500. I32u tfwrite(ptr, size, n, stream)
  501. I8s Hp  ptr;
  502. I32s  size, n;
  503. FILE *  stream;
  504. {   I32u  r_size = 0;
  505. #ifdef __TURBOC__
  506.     I32s  segs, rem, i = 0;
  507.  
  508.     segs = n / (I32s) UINT_MAX;
  509.     rem  = n % (I32s) UINT_MAX;
  510.     if(segs) for(i = 0; i < segs; i++)
  511.         r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
  512.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  513.     if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
  514.         (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  515. #endif
  516.  
  517. #ifdef OS2_MC
  518.     I32s  segs, rem, i = 0;
  519.  
  520.     segs = n / (I32s) UINT_MAX;
  521.     rem  = n % (I32s) UINT_MAX;
  522.     if(segs) for(i = 0; i < segs; i++)
  523.         r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
  524.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  525.     if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
  526.         (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  527. #endif
  528.  
  529. #ifdef unix
  530.     r_size = fwrite(ptr, size, n, stream);
  531. #endif
  532.  
  533. #ifdef IBM3090
  534.     r_size = fwrite(ptr, size, n, stream);
  535. #endif
  536.  
  537.     if(r_size != n)
  538.     {   
  539. #ifdef ARG 
  540.         fprintf(stderr, 
  541.           "Tierra tfwrite() inconsistency: n = %ld  r_size = %ld", n, r_size);
  542. #else
  543.         FEError(-707,NOEXIT,NOWRITE,
  544.           "Tierra tfwrite() inconsistency: n = %ld  r_size = %ld", n, r_size);
  545. #endif
  546.     }
  547.     return r_size;
  548. }
  549.